package com.xialing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xialing.controller.request.AccountLoginRequest;
import com.xialing.controller.request.AccountRegisterRequest;
import com.xialing.enums.AuthTypeEnum;
import com.xialing.enums.BizCodeEnum;
import com.xialing.enums.SendCodeEnum;
import com.xialing.manager.AccountManager;
import com.xialing.mapper.AccountMapper;
import com.xialing.model.AccountDO;
import com.xialing.model.LoginUser;
import com.xialing.service.AccountService;
import com.xialing.service.NotifyService;
import com.xialing.util.CommonUtil;
import com.xialing.util.IDUtil;
import com.xialing.util.JWTUtil;
import com.xialing.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
@Slf4j
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountDO> implements AccountService {

    @Autowired
    private NotifyService notifyService;

    @Autowired
    private AccountManager accountManager;


    /**
     *  注册
     * @param accountRegisterRequest
     * @return
     */
    @Override
    public JsonData register(AccountRegisterRequest accountRegisterRequest) {
        // 校验验证码
        boolean checkCode = false;
        if (accountRegisterRequest != null) {
            checkCode = notifyService.checkCode(SendCodeEnum.USER_REGISTER, accountRegisterRequest.getPhone(), accountRegisterRequest.getCode());
        }
        if (!checkCode) {
            return JsonData.buildResult(BizCodeEnum.CODE_ERROR);
        }

        AccountDO accountDO = new AccountDO();
        BeanUtils.copyProperties(accountRegisterRequest,accountDO);
        accountDO.setAuth(AuthTypeEnum.DEFAULT.name());

        // 生成唯一账号
        accountDO.setAccountNo(Long.valueOf(IDUtil.geneSnowFlakeID().toString()));

        // 设置密码，密钥 加盐
        accountDO.setSecret("$1$" + CommonUtil.getStringNumRandom(8));
        String cryptPwd = Md5Crypt.md5Crypt(accountRegisterRequest.getPwd().getBytes(), accountDO.getSecret());
        accountDO.setPwd(cryptPwd);

        int row = accountManager.insert(accountDO);
        log.info("注册成功：{}", row);

        // 注册成功发放福利 todo
        userRegisterInitTask(accountDO);


        return JsonData.buildSuccess();
    }



    /**
     * 登录
     * @param accountLoginRequest
     * @return
     */
    @Override
    public JsonData login(AccountLoginRequest accountLoginRequest) {

        List<AccountDO> accountDOList = accountManager.findByPhone(accountLoginRequest.getPhone());

        if (accountDOList != null && accountDOList.size() == 1) {
            AccountDO accountDO = accountDOList.get(0);

            String md5Crypt = Md5Crypt.md5Crypt(accountLoginRequest.getPwd().getBytes(), accountDO.getSecret());
            if (md5Crypt.equalsIgnoreCase(accountDO.getPwd())) {
                LoginUser loginUser = new LoginUser();
                BeanUtils.copyProperties(accountDO, loginUser);
                String token = JWTUtil.geneJsonWebToken(loginUser);

                return JsonData.buildSuccess(token);
            } else {
                return JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
            }
        } else {
            return JsonData.buildResult(BizCodeEnum.ACCOUNT_UNREGISTER);
        }

    }




    /**
     * 用户初始化，发放福利：流量包 TODO
     * @param accountDO
     */
    private void userRegisterInitTask(AccountDO accountDO) {

    }
}
